﻿using System.Diagnostics;
using DimensionsHelper.LanguageService.Symbols;
using DimensionsHelper.LanguageService.Syntax;

namespace DimensionsHelper.LanguageService;

/// <summary>
///     语法分析树，在<see cref="SyntaxTree" />语法树基础上进行语义分析的
///     语法树。
/// </summary>
public class AnalysisTree : AnalysisNode
{
    
    private readonly List<AnalysisStatement> _nodes = [];
    
    
    internal AnalysisTree(WorkFolder workFolder, SyntaxTree syntax) :
        base(AnalysisBoundKinds.Tree, syntax, null)
    {
        Syntax = syntax;
        WorkFolder = workFolder;
        Symbols = new AnalysisTreeSymbolTable(syntax);
    }
    

    /// <summary>
    ///     原始语法树。
    /// </summary>
    public override SyntaxTree Syntax { get; }

    
    /// <summary>
    ///     语法树所在的工作文件夹
    /// </summary>
    public WorkFolder WorkFolder { get; }
    
    
    internal AnalysisTreeSymbolTable Symbols { get; }
    
    public override int ChildrenCount => _nodes.Count;
    
    public override bool IsExpression => false;

    public override bool IsStatement => false;

    public override bool IsMetadata => false;
    

    /// <summary>
    ///     获取语法树节点列表
    /// </summary>
    public override IEnumerable<AnalysisNode> GetChildren()
    {
        return _nodes;
    }


    /// <summary>
    ///     向节点列表末尾添加一个语句节点。
    /// </summary>
    internal override void AddChild(AnalysisNode node)
    {
        Debug.Assert(node.IsStatement);
        _nodes.Add((AnalysisStatement)node);
    }
}


public static partial class AnalysisNodeFactory
{
    public static AnalysisTree Tree(WorkFolder workFolder, SyntaxTree syntax)
    {
        AnalysisTree analysisTree = new(workFolder, syntax);
        return analysisTree;
    }
}